package com.haiyou.dc.quartz.controller;

import com.github.pagehelper.PageInfo;
import com.haiyou.common.util.R;
import com.haiyou.dc.quartz.entity.ScheduleJob;
import com.haiyou.dc.quartz.entity.dto.ScheduleJobDTO;
import com.haiyou.dc.quartz.schedule.JobSchedulerManager;
import com.haiyou.dc.quartz.service.JobScheduleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 任务调度管理
 */
@RestController
@RequestMapping(value = "/schedule")
@Slf4j
@Api(tags = "Quartz集群接口")
public class ScheduleController {

    @Autowired
    private JobScheduleService jobScheduleService;

    @Autowired
    private JobSchedulerManager jobSchedulerManager;

    @ApiOperation(value = "添加调度任务")
    @PostMapping("/add")
    public R addJob(@RequestBody ScheduleJobDTO job) {
        try {
            jobSchedulerManager.addJob(job.getJobName(), job.getJobGroup(), job.getCronExpression());
            return R.ok();
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getLocalizedMessage());
            return R.error();
        }
    }

    @ApiOperation(value = "暂停调度任务")
    @PostMapping("/pause")
    public R pauseJob(@RequestBody ScheduleJob job) {
        try {
            jobSchedulerManager.jobPause(job.getJobName(), job.getJobGroup());
            return R.ok();
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getLocalizedMessage());
            return R.error();
        }
    }

    @ApiOperation(value = "重新启动调度任务")
    @PostMapping("/resume")
    public R resumeJob(@RequestBody ScheduleJob job) {
        try {
            jobSchedulerManager.jobResume(job.getJobName(), job.getJobGroup());
            return R.ok();
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getLocalizedMessage());
            return R.error();
        }
    }

    @ApiOperation(value = "修改调度任务")
    @PostMapping("/reschedule")
    public R rescheduleJob(@RequestBody ScheduleJobDTO job) {
        try {
            jobSchedulerManager.jobReschedule(job.getJobName(), job.getJobGroup(), job.getCronExpression());
            return R.ok();
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getLocalizedMessage());
            return R.error();
        }
    }

    @ApiOperation(value = "删除调度任务")
    @PostMapping("/delete")
    public R deleteJob(@RequestBody ScheduleJob job) {
        try {
            jobSchedulerManager.jobDelete(job.getJobName(), job.getJobGroup());
            return R.ok();
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getLocalizedMessage());
            return R.error();
        }
    }

    @ApiOperation(value = "立即执行调度任务")
    @PostMapping("/trigger")
    public R triggerJob(@RequestBody ScheduleJob job) {
        try {
            jobSchedulerManager.triggerJob(job.getJobName(), job.getJobGroup());
            return R.ok();
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getLocalizedMessage());
            return R.error();
        }
    }

    @ApiOperation(value = "调度任务分页列表")
    @PostMapping("/all")
    public R page(@RequestBody ScheduleJob scheduleJob, @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                  @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {
        try {
            PageInfo<ScheduleJob> jobAndTrigger = jobScheduleService.getJobAndTriggerDetails(scheduleJob, pageNum, pageSize);
            return R.ok(jobAndTrigger);
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getLocalizedMessage());
            return R.error();
        }
    }
}
